package com.ruoyi.project.app.aspect.handler;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.app.anno.NotEmpty;

import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Parameter;

/**
 * 处理@NotEmpty
 */
public class NotEmptyHandler implements Handler{
    @Override
    public boolean handler(AnnotatedElement element,Object obj) {
        Class<?> clazzType = null;
        NotEmpty notEmpty = null;
        if(element instanceof Parameter){
            Parameter  parameter = (Parameter) element;
            clazzType = parameter.getType();
            notEmpty = parameter.getAnnotation(NotEmpty.class);
        }else if(element instanceof Field){
            Field field = (Field) element;
            clazzType = field.getType();
            notEmpty = field.getAnnotation(NotEmpty.class);
        }

        if(clazzType==String.class&&notEmpty != null&&StringUtils.isInvalid((String) obj)){
            throw new RuntimeException(notEmpty.msg());
        }
        return false;
    }
}
